package com.mbc.rmt.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.mbc.common.dao.DAO;
import com.mbc.rmt.data.WaitingCOADataList;
import com.mbc.rmt.entities.WaitingCOA;
import com.mbc.rmt.entities.WaitingCOARpt;

public class WaitingCOADAOImpls extends DAO<WaitingCOA> implements
		WaitingCOADAO {

	public WaitingCOADAOImpls(Connection conn) {
		this.conn = conn;
		this.table_name = "WaitingCOA";
	}

	@Override
	public void create(WaitingCOA entity) throws SQLException {
		query = "insert into "
				+ table_name
				+ " ( waiting_coa_id, waiting_coa_code, description, accpayable, unrealize_ex_gl, write_off, accpayable_dept, unrealize_ex_gl_dept, write_off_dept, BranchId) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		pstmt = conn.prepareStatement(query);
		int i = 1;
		pstmt.setLong(i++, entity.getWaiting_coa_id());
		pstmt.setString(i++, entity.getWaiting_coa_code());
		pstmt.setString(i++, entity.getDescription());
		pstmt.setLong(i++, entity.getAccpayable());
		pstmt.setLong(i++, entity.getUnrealize_ex_gl());
		pstmt.setLong(i++, entity.getWrite_off());
		pstmt.setLong(i++, entity.getAccpayable_dept());
		pstmt.setLong(i++, entity.getUnrealize_ex_gl_dept());
		pstmt.setLong(i++, entity.getWrite_off_dept());
		pstmt.setLong(i++, entity.getBranchId());
		pstmt.executeUpdate();
		pstmt.close();

	}

	@Override
	public void update(WaitingCOA entity) throws SQLException {
		query = "update "
				+ table_name
				+ " set waiting_coa_code = ?, description = ?, accpayable = ?, unrealize_ex_gl = ?, write_off = ?, accpayable_dept = ?, unrealize_ex_gl_dept = ?, write_off_dept = ?, BranchId = ?"
				+ "where waiting_coa_id = " + entity.getWaiting_coa_id();
		pstmt = conn.prepareStatement(query);
		int i = 1;
		pstmt.setString(i++, entity.getWaiting_coa_code());
		pstmt.setString(i++, entity.getDescription());
		pstmt.setLong(i++, entity.getAccpayable());
		pstmt.setLong(i++, entity.getUnrealize_ex_gl());
		pstmt.setLong(i++, entity.getWrite_off());
		pstmt.setLong(i++, entity.getAccpayable_dept());
		pstmt.setLong(i++, entity.getUnrealize_ex_gl_dept());
		pstmt.setLong(i++, entity.getWrite_off_dept());
		pstmt.setLong(i++, entity.getBranchId());
		pstmt.executeUpdate();
		pstmt.close();

	}

	@Override
	public void delete(long key) throws SQLException {
		query = getDeleteQuery() + " where waiting_coa_id = " + key;
		pstmt = conn.prepareStatement(query);
		pstmt.executeUpdate();
		pstmt.close();

	}

	@Override
	public WaitingCOA find(long key) throws SQLException {
		WaitingCOA l_WaitingCOA = null;
		// query = getSelectQuery() + " where agent_coa_id = " + key;
		query = getSelectQuery()
				+ "acoa.waiting_coa_id,acoa.waiting_coa_code,acoa.description, "
				+ "acoa.accpayable,acrcoa.AC_ID accpayable_id, acoa.accpayable_dept,acrdep.Dept_ID accpayable_dept_id, "
				+ "acoa.unrealize_ex_gl,urcoa.AC_ID unrealize_ex_gl_id,acoa.unrealize_ex_gl_dept,urdep.Dept_ID unrealize_ex_gl_dept_id, "
				+ "acoa.write_off,wcoa.AC_ID write_off_id,acoa.write_off_dept,wdep.Dept_ID write_off_dept_id "
				+ "from dbo.WaitingCOA acoa "
				+ "LEFT JOIN dbo.COA acrcoa ON acoa.accpayable=acrcoa.COAKey "
				+ "LEFT JOIN dbo.Department acrdep ON acoa.accpayable_dept=acrdep.DepartmentKey "
				+ "LEFT JOIN dbo.COA urcoa ON acoa.unrealize_ex_gl=urcoa.COAKey "
				+ "LEFT JOIN dbo.Department urdep ON acoa.unrealize_ex_gl_dept=urdep.DepartmentKey "
				+ "LEFT JOIN dbo.COA wcoa ON acoa.write_off=wcoa.COAKey "
				+ "LEFT JOIN dbo.Department wdep ON acoa.write_off_dept=wdep.DepartmentKey "
				+ " where waiting_coa_id = " + key;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if (rs.next())
			l_WaitingCOA = readRecord(rs);
		rs.close();
		pstmt.close();
		return l_WaitingCOA;
	}

	@Override
	public WaitingCOA findByCode(String code,long branchId) throws SQLException {
		WaitingCOA l_WaitingCOA = null;
		query = " select row_number() over(order by waiting_coa_id desc)as [rowno], "
				+ "acoa.waiting_coa_id,acoa.waiting_coa_code,acoa.description,acoa.BranchId, "
				+ "acoa.accpayable,acrcoa.AC_ID accpayable_code, acoa.accpayable_dept,acrdep.departmentCode accpayable_dept_code, "
				+ "acoa.unrealize_ex_gl,urcoa.AC_ID unrealize_ex_gl_code,acoa.unrealize_ex_gl_dept,urdep.departmentCode unrealize_ex_gl_dept_code, "
				+ "acoa.write_off,wcoa.AC_ID write_off_code,acoa.write_off_dept,wdep.departmentCode write_off_dept_code "
				+ "from dbo.WaitingCOA acoa "
				+ "LEFT JOIN dbo.COA acrcoa ON acoa.accpayable=acrcoa.COAKey "
				+ "LEFT JOIN dbo.Department acrdep ON acoa.accpayable_dept=acrdep.departmentID "
				+ "LEFT JOIN dbo.COA urcoa ON acoa.unrealize_ex_gl=urcoa.COAKey "
				+ "LEFT JOIN dbo.Department urdep ON acoa.unrealize_ex_gl_dept=urdep.departmentID "
				+ "LEFT JOIN dbo.COA wcoa ON acoa.write_off=wcoa.COAKey "
				+ "LEFT JOIN dbo.Department wdep ON acoa.write_off_dept=wdep.departmentID "
				+ " where waiting_coa_code = '" + code 
				+ "' and acoa.BranchId = " + branchId;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if (rs.next())
			l_WaitingCOA = readRecord(rs);
		rs.close();
		pstmt.close();
		return l_WaitingCOA;
	}

	@Override
	public ArrayList<WaitingCOA> findAll() throws SQLException {
		ArrayList<WaitingCOA> l_WaitingCOAList = new ArrayList<WaitingCOA>();
		query = getSelectQuery();
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		while (rs.next()) {
			l_WaitingCOAList.add(readRecord(rs));
		}
		rs.close();
		pstmt.close();
		return l_WaitingCOAList;
	}

	@Override
	public WaitingCOADataList findWaitingCOAs(WaitingCOA cri)
			throws SQLException {
		WaitingCOADataList l_DataList = new WaitingCOADataList();
		ArrayList<WaitingCOA> l_entityList = new ArrayList<WaitingCOA>();
		String l_cri = " and " + cri.getFieldname() + " like N'%" + cri.getKeyword() + "%'" +
//				"and (waiting_coa_code like N'%" + cri.getKeyword()
//				+ "%'" + " or acoa.description like N'%" + cri.getKeyword()+ "%')" +
				" and acoa.BranchId = " + cri.getBranchId();

		query = "set rowcount "
				+ cri.getSize()
				+ " select * from (select row_number() over(order by waiting_coa_id desc)as [rowno], "
				+ "acoa.waiting_coa_id,acoa.waiting_coa_code,acoa.description,acoa.BranchId, "
				+ "acoa.accpayable,acrcoa.AC_ID accpayable_code, acoa.accpayable_dept,acrdep.departmentCode accpayable_dept_code, "
				+ "acoa.unrealize_ex_gl,urcoa.AC_ID unrealize_ex_gl_code,acoa.unrealize_ex_gl_dept,urdep.departmentCode unrealize_ex_gl_dept_code, "
				+ "acoa.write_off,wcoa.AC_ID write_off_code,acoa.write_off_dept,wdep.departmentCode write_off_dept_code "
				+ "from dbo.WaitingCOA acoa "
				+ "LEFT JOIN dbo.COA acrcoa ON acoa.accpayable=acrcoa.COAKey "
				+ "LEFT JOIN dbo.Department acrdep ON acoa.accpayable_dept=acrdep.departmentID "
				+ "LEFT JOIN dbo.COA urcoa ON acoa.unrealize_ex_gl=urcoa.COAKey "
				+ "LEFT JOIN dbo.Department urdep ON acoa.unrealize_ex_gl_dept=urdep.departmentID "
				+ "LEFT JOIN dbo.COA wcoa ON acoa.write_off=wcoa.COAKey "
				+ "LEFT JOIN dbo.Department wdep ON acoa.write_off_dept=wdep.departmentID "
				+ "where 1 = 1" + l_cri + ") " + table_name + " where rowno > "
				+ cri.getIndex();
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		while (rs.next()) {
			l_entityList.add(readRecord(rs));
		}
		l_DataList.setEntityList(l_entityList);
		rs.close();
		pstmt.close();
		String l_cri1 = "and (waiting_coa_code like N'%" + cri.getKeyword()
				+ "%'" + " or description like N'%" + cri.getKeyword() + "%')" +
				" and BranchId = " + cri.getBranchId();
		query = "select count(waiting_coa_id) from " + table_name
				+ " where 1 = 1" + l_cri1;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if (rs.next()) {
			l_DataList.setTotalRecord(rs.getLong(1));
		}
		rs.close();
		pstmt.close();
		return l_DataList;
	}

	@Override
	protected WaitingCOA readRecord(ResultSet arg0) throws SQLException {
		WaitingCOA l_entity = new WaitingCOA();
		l_entity.setWaiting_coa_id(rs.getLong("waiting_coa_id"));
		l_entity.setWaiting_coa_code(rs.getString("waiting_coa_code"));
		l_entity.setDescription(rs.getString("description"));

		l_entity.setAccpayable(rs.getLong("accpayable"));
		l_entity.setAccpayable_code(rs.getString("accpayable_code"));

		l_entity.setUnrealize_ex_gl(rs.getLong("unrealize_ex_gl"));
		l_entity.setUnrealize_ex_gl_code(rs.getString("unrealize_ex_gl_code"));

		l_entity.setWrite_off(rs.getLong("write_off"));
		l_entity.setWrite_off_code(rs.getString("write_off_code"));

		l_entity.setAccpayable_dept(rs.getLong("accpayable_dept"));
		l_entity.setAccpayable_dept_code(rs.getString("accpayable_dept_code"));

		l_entity.setUnrealize_ex_gl_dept(rs.getLong("unrealize_ex_gl_dept"));
		l_entity.setUnrealize_ex_gl_dept_code(rs
				.getString("unrealize_ex_gl_dept_code"));

		l_entity.setWrite_off_dept(rs.getLong("write_off_dept"));
		l_entity.setWrite_off_dept_code(rs.getString("write_off_dept_code"));

		l_entity.setBranchId(rs.getLong("BranchId"));
		return l_entity;
	}

	public ResultSet prepareWaitingCOAReport(WaitingCOARpt info
			) throws SQLException {
		ResultSet l_rs;
		String l_query = "dbo.sp_cr_WaitingCOARpt ?, ?, ?";

		CallableStatement l_pstmt = conn.prepareCall(l_query);
		WaitingCOARptParameter(l_pstmt, info);
		l_rs = l_pstmt.executeQuery();
		return l_rs;

	}

	private void WaitingCOARptParameter(CallableStatement l_pstmt,
			WaitingCOARpt info) throws SQLException {
		int i = 1;

		l_pstmt.setString(i++, info.getFromWaitingCOAID());
		l_pstmt.setString(i++, info.getToWaitingCOAID());
		l_pstmt.setLong(i++, info.getBranchId());
	}

	@Override
	public WaitingCOADataList getWaitingCOAList(WaitingCOARpt info)
			throws SQLException {
		WaitingCOADataList l_DataList = new WaitingCOADataList();
		ArrayList<WaitingCOA> l_WaitingCOAList = new ArrayList<WaitingCOA>();
		String l_query = "dbo.sp_cr_WaitingCOARpt ?, ?, ?";
		CallableStatement l_pstmt = conn.prepareCall(l_query);
		WaitingCOARptParameter(l_pstmt, info);
		ResultSet l_rs = l_pstmt.executeQuery();
		while (l_rs.next()) {
			l_WaitingCOAList.add(readRecordFT(l_rs));
		}
		l_DataList.setEntityList(l_WaitingCOAList);
		l_rs.close();
		l_pstmt.close();
		return l_DataList;
	}
	
	private WaitingCOA readRecordFT(ResultSet rs) throws SQLException {
		WaitingCOA l_entity = new WaitingCOA();
		
		l_entity.setWaiting_coa_code(rs.getString("waiting_coa_code"));
		l_entity.setDescription(rs.getString("description"));

		l_entity.setAccpayable_code(rs.getString("accpayable_code"));

		l_entity.setUnrealize_ex_gl_code(rs.getString("unrealize_ex_gl_code"));

		l_entity.setWrite_off_code(rs.getString("write_off_code"));

		l_entity.setAccpayable_dept_code(rs.getString("accpayable_dept_code"));

		l_entity.setUnrealize_ex_gl_dept_code(rs
				.getString("unrealize_ex_gl_dept_code"));

		l_entity.setWrite_off_dept_code(rs.getString("write_off_dept_code"));
		
		return l_entity;
	}

}
